package com.ffyc.cms.controller;

import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.Admin;
import com.ffyc.cms.model.News;
import com.ffyc.cms.model.Newstype;
import com.ffyc.cms.service.NewsService;
import com.ffyc.cms.service.NewstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping(path = "news")
public class NewsController {
    @Autowired
    NewsService newsService;
    /**
     * 查询出所有的导航
     * @return
     */
    @RequestMapping(path = "getnewsList")
    public CommonResult getnewtpyeList(Integer id){
        CommonResult commonResult=null;
        try{
            List<News> newstypeslist = newsService.getnewsList(id);
            if (newstypeslist==null){
                commonResult=new CommonResult(201,"新闻类型查询为空",null);
            }else {
                commonResult=new CommonResult(200,"新闻类型查询成功",newstypeslist);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"新闻类型查询失败",null);
        }
        return commonResult;
    }

    /**
     * 根据前端用户点击的新闻查出新闻的内容
     * @param id 用户点击的新闻的id
     * @return
     */
    @RequestMapping(path = "getnewscontent")
    public CommonResult getnewscontent(Integer id){
        CommonResult commonResult=null;
        try{
            News newscontent = newsService.getnewscontent(id);
            if (newscontent==null){
                commonResult=new CommonResult(201,"新闻内容查询为空",null);
            }else {
                commonResult=new CommonResult(200,"新闻内容查询成功",newscontent);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"新闻内容查询失败",null);
        }
        return commonResult;
    }


    //将搜索表单的内容往前传
    @RequestMapping(value = "serch")
    public CommonResult serch(News news){
        CommonResult commonResult=null;
        try {
            List<News> newsList=newsService.serch(news);
            commonResult=new CommonResult(200,"关键字查询成功",newsList);
        }catch (Exception e){
            commonResult=new CommonResult(500,"关键字查询失败",null);
        }
        return commonResult;

    }

    /**
     * 查询出点赞数最多的功能
     * @return
     */
    @RequestMapping(value = "getmaxpraise")
    public CommonResult getmaxpraise(){
        CommonResult commonResult=null;
        try{
            List<News> maxlist =newsService.getmaxpraise();
            if (maxlist==null){
                commonResult=new CommonResult(201,"点击最多的功能查询为空",null);
            }else {
                commonResult=new CommonResult(200,"点击最多的功能查询成功",maxlist);
            }

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(200,"点击最多的功能查询失败",null);
        }
        return commonResult;
    }

    /**
     * 查询出点赞最多的新闻
     * @return
     */
    @RequestMapping(value = "getmaxlike")
    public CommonResult getmaxlike(){
        CommonResult commonResult=null;
        try{
            List<News> maxlikelist =newsService.getmaxlike();
            if (maxlikelist==null){
                commonResult=new CommonResult(201,"点赞最多的功能查询为空",null);
            }else {
                commonResult=new CommonResult(200,"点赞最多的功能查询成功",maxlikelist);
            }

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(200,"点赞最多的功能查询失败",null);
        }
        return commonResult;
    }


    /**
     * 查询最新发布的文章
     * @return
     */
    @RequestMapping(value = "getlatestnews")
    public CommonResult getlatestnews(){
        CommonResult commonResult=null;
        try{
            List<News> getlatestlist =newsService.getlatestnews();
            if (getlatestlist==null){
                commonResult=new CommonResult(201,"最新发布的功能查询为空",null);
            }else {
                commonResult=new CommonResult(200,"最新发布的功能查询成功",getlatestlist);
            }

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(200,"最新发布的功能查询失败",null);
        }
        return commonResult;
    }



    /**
     * 查询新闻的前五张图片,显示到滑动窗口(按新闻点赞排名)
     * @return
     */
    @RequestMapping(value = "getimglist")
    public CommonResult getimglist(){
        CommonResult commonResult=null;
        try{
            List<News> imglikelist =newsService.getimglist();
            if (imglikelist==null){
                commonResult=new CommonResult(201,"最多的新闻封面功能查询为空",null);
            }else {
                commonResult=new CommonResult(200,"最多的新闻封面功能查询成功",imglikelist);
            }

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(200,"最多的新闻封面功能查询失败",null);
        }
        return commonResult;
    }

}
